package com.credithc.customers.manager.utils;

import org.apache.commons.lang.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author zhukai
 * @date 2018/12/18 13:58
 */
public class StatsUtils {

    public static List<String> generateXAxisList(String start, String end, String type) {
        if ("week".equals(type)) {
            return getList(6, null);
        } else if ("customized".equals(type)) {
            return getList(subDate(start, end), end);
        } else {
            return getList(29, null);
        }
    }

    public static ArrayList<String> getList(int intervals, String dateStr) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        for (int i = intervals; i >= 0; i--) {
            pastDaysList.add(getPastDate(i, dateStr));
        }
        return pastDaysList;
    }

    public static String getPastDate(int past, String dateStr) {
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] date = dateStr.split("-");
            calendar.set(Integer.valueOf(date[0]), Integer.valueOf(date[1]) - 1, Integer.valueOf(date[2]));
        }
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }
    public static String getPastDateTime(int past) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -past);
        date = calendar.getTime();
        String result = sdf.format(date);
        return result;
    }

    public static String getPostDate(int post, String dateStr) {
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] date = dateStr.split("-");
            calendar.set(Integer.valueOf(date[0]), Integer.valueOf(date[1]) - 1, Integer.valueOf(date[2]));
        }
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + post);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    public static int subDate(String startDate, String endDate) {
        Calendar beginCalendar = Calendar.getInstance();
        String[] start = startDate.split("-");
        beginCalendar.set(Integer.valueOf(start[0]), Integer.valueOf(start[1]), Integer.valueOf(start[2]));
        Calendar endCalendar = Calendar.getInstance();
        String[] end = endDate.split("-");
        endCalendar.set(Integer.valueOf(end[0]), Integer.valueOf(end[1]), Integer.valueOf(end[2]));
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        int betweenDays = (int) (((endTime - beginTime) / (1000 * 60 * 60 * 24)));
        return betweenDays;
    }

    public static String getCurrentTime() {
        String returnStr = null;
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        returnStr = f.format(date);
        return returnStr;
    }

    public static String timestamp2Stirng(Timestamp time) {
        if(time == null){
            return null;
        }
        String tsStr = null;
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            tsStr = sdf.format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tsStr;
    }
}
